﻿using System.Text;
using System.Collections.Specialized;
using Newtonsoft.Json;

namespace CometDocs
{
    internal partial class Client
    {
        /// <summary>
        /// Delete a file: https://www.cometdocs.com/developer/apiDocumentation#method-deleteFile
        /// </summary>
        /// <param name="token">A valid authentication token.</param>
        /// <param name="file">The file to delete.</param>
        /// <param name="deleteRevisions">True if all the file revisions should be deleted too.</param>
        public void DeleteFile(AuthenticationToken token, FileInfo file, bool? deleteRevisions = null)
        {
            NameValueCollection @params = new NameValueCollection(3);
            @params.Add("token", token.Value);
            @params.Add("id", file.ID.ToString());
            if (deleteRevisions != null)
            {
                @params.Add("deleteRevisions", deleteRevisions.Value ? "1" : "0");
            }

            byte[] result = webClient.UploadValues(APIRoot + "deleteFile", @params);

            string json = Encoding.ASCII.GetString(result);

            Response response = JsonConvert.DeserializeObject<Response>(json);

            CheckAndThrow(response);
        }
    }
}
